package geektime.nio.smtp;
/**
 *
 **/

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Map;

/**
 * @Author weiwei
 * @Date 2022-01-10 10:09
 * @description
 **/
public class ServiceHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        SMTProcess.processMap.put(ctx.channel().id().asLongText(),new SMTProcess());
        ctx.fireChannelActive();
        ctx.writeAndFlush("220 Netty SMTP Server");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        SMTProcess process = SMTProcess.processMap.get(ctx.channel().id().asLongText());
        ctx.write(process.backMsg((String) msg));
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}
